package com.example.dell.library.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.dell.library.R;
import com.example.dell.library.bean.Student;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class PasswordActivity extends AppCompatActivity {
    @BindView(R.id.ori_pass)
    EditText oriPass;
    @BindView(R.id.new_pass)
    EditText newPass;
    @BindView(R.id.update_pass)
    Button updatePass;
    private String objectId;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        ButterKnife.bind(this);

        objectId = getIntent().getStringExtra("Id");
        password = getIntent().getStringExtra("Password");
        updatePass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changePassword();
            }
        });
    }

    //修改密码
    private void changePassword() {
        String newPassword = newPass.getText().toString();
        Student student = new Student();
        student.setPassword(newPassword);
        final String oriPassword = oriPass.getText().toString();
        if ("".equals(oriPassword) || !oriPassword.equals(password) || "".equals(newPassword)) {
            Toast.makeText(PasswordActivity.this, "修改失败,请重新输入原密码或新密码", Toast.LENGTH_SHORT).show();
        } else {
            student.update(objectId, new UpdateListener() {
                @Override
                public void done(BmobException e) {
                    if (e == null) {
                        Toast.makeText(PasswordActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }
                }
            });
        }
    }
}
